/*
 * @Description: 
 * @FilePath: /STM32F103/USER/stm32f10x_it.c
 * @Author: Wei Zhou
 * @Github: https://github.com/zromyk
 * @Date: 2018-10-12 17:21:34
 * @LastEditors: Wei Zhou
 * @LastEditTime: 2021-05-13 00:18:58
 * @Copyright: Copyright © 2017 muyiro. All rights reserved.
 */
/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.4.0
  * @date    10/15/2010
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "gpio.h"
#include "tim.h"
#include "dma.h"
#include "uart.h"
#include "spi.h"

/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void TIM6_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM6, TIM_IT_Update) == SET)
    {
        TIM_ClearITPendingBit(TIM6, TIM_IT_Update);  /* 清除标志         */
    }
}

void TIM7_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM7, TIM_IT_Update) == SET) 
    {
        TIM_ClearITPendingBit(TIM7, TIM_IT_Update);  /* 清除标志 */
    }
}


/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

static void USART_IRQHandler_Template(USART_Dev *usart, void (*func)(void))
{
    USART_TypeDef* USARTx = usart->USARTx;
    
    /* 清除标志位：先读 SR 寄存器，在读 DR 寄存器 */
    /* USART 接收缓冲区非空中断 */
    if (USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
    {
        USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
        usart->rx_buf[0] = USART_ReceiveData(USARTx);
        
        func();
    }
    
    /* 清除标志位：先读 SR 寄存器，在读 DR 寄存器 */
    /* USART 总线空闲中断：从忙碌到空闲 */
    if (USART_GetITStatus(USARTx, USART_IT_IDLE) != RESET)
    {
        /* USART_ClearITPendingBit(USARTx, USART_IT_IDLE); */
        USART_ReceiveData(USARTx);
        DMA_Dev* dma = &usart->dma;
        
        DMA_Cmd(dma->rx_channel, DISABLE); /* 关闭DMA */
        usart->rx_cnt = usart->rx_buf_size - DMA_GetCurrDataCounter(dma->rx_channel); //得到真正接收数据个数
        
        func();
        
        DMA_ClearFlag(dma->rx_FLAG_TC); /* 清除传输完成标志 */
        /* usart1.dma.rx_channel->NDTR = usart1.rx_buf_size; /* 重新设置接收数据个数         */
        DMA_Cmd(dma->rx_channel, ENABLE);  /* 开启DMA */
    }
    
    /* 溢出：如果发生溢出，需要先读SR，再读DR寄存器，则可以清除不进入中断的问题 */
    if (USART_GetFlagStatus(USARTx, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(USARTx, USART_FLAG_ORE);
        USART_ReceiveData(USARTx);
    }
}

static void do_nothing(void)
{
    /* do_nothing... */
}

/**
  * @brief  This function handles USART1 global interrupt request.
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
    USART_IRQHandler_Template(&usart1, &do_nothing);
}

/**
 * @brief  This function handles USART2 global interrupt request.
 * @param  None
 * @retval None
 */
void USART2_IRQHandler(void)
{
    USART_IRQHandler_Template(&usart2, &do_nothing);
}

/**
  * @brief  This function handles USART3 global interrupt request.
  * @param  None
  * @retval None
  */
void USART3_IRQHandler(void)
{
    USART_IRQHandler_Template(&usart3, &do_nothing);
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void SPI1_IRQHandler(void)
{
    if (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) != RESET) 
    {
        SPI_I2S_ClearFlag(SPI1, SPI_I2S_FLAG_RXNE); /* 一定要清除标志位 */
        spi1.rx_buf[spi1.rx_cnt] = SPI_I2S_ReceiveData(SPI1);
        if (spi1.rx_cnt < spi1.rx_buf_size) {
            spi1.rx_cnt++;
        } else {
            usart_reply(spi1.rx_buf, spi1.rx_cnt);
            spi1.rx_cnt = 0;
        }
    }
}

void SPI2_IRQHandler(void)
{
    if (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) != RESET)
    {
        SPI_I2S_ClearFlag(SPI2, SPI_I2S_FLAG_RXNE); /* 一定要清除标志位 */
        spi2.rx_buf[spi2.rx_cnt] = SPI_I2S_ReceiveData(SPI2);
        if (spi2.rx_cnt < spi2.rx_buf_size) {
            spi2.rx_cnt++;
        } else {
            usart_reply(spi2.rx_buf, spi2.rx_cnt);
            spi2.rx_cnt = 0;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
  * @brief  DMA 发送完成中�? 发送完毕之后触发中�? 然后将总线切换到接收模�?
  * @param  None
  * @retval None
  */

void DMA1_Channel1_IRQHandler(void)
{
    /* 传输完成中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TC1) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TC1);
        DMA_Cmd(DMA1_Channel1, DISABLE);
    }
    /* 传输错误中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TE1) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TE1);
        DMA_Cmd(DMA1_Channel1, DISABLE);
    }
}

void DMA1_Channel2_IRQHandler(void)
{
    /* 传输完成中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TC2) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TC2);
        DMA_Cmd(DMA1_Channel2, DISABLE);
    }
    /* 传输错误中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TE2) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TE2);
        DMA_Cmd(DMA1_Channel2, DISABLE);
    }
}

void DMA1_Channel3_IRQHandler(void)
{
    /* 传输完成中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TC3) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TC3);
        DMA_Cmd(DMA1_Channel3, DISABLE);
    }
    /* 传输错误中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TE3) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TE3);
        DMA_Cmd(DMA1_Channel3, DISABLE);
    }
}

void DMA1_Channel4_IRQHandler(void)
{
    /* 传输完成中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TC4) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TC4);
        DMA_Cmd(DMA1_Channel4, DISABLE);
    }
    /* 传输错误中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TE4) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TE4);
        DMA_Cmd(DMA1_Channel4, DISABLE);
    }
}

void DMA1_Channel5_IRQHandler(void)
{
    /* 传输完成中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TC5) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TC5);
        DMA_Cmd(DMA1_Channel5, DISABLE);
    }
    /* 传输错误中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TE5) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TE5);
        DMA_Cmd(DMA1_Channel5, DISABLE);
    }
}

void DMA1_Channel6_IRQHandler(void)
{
    /* 传输完成中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TC6) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TC6);
        DMA_Cmd(DMA1_Channel6, DISABLE);
    }
    /* 传输错误中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TE6) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TE6);
        DMA_Cmd(DMA1_Channel6, DISABLE);
    }
}

void DMA1_Channel7_IRQHandler(void)
{
    /* 传输完成中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TC7) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TC7);
        DMA_Cmd(DMA1_Channel7, DISABLE);
    }
    /* 传输错误中断 */
    if (DMA_GetFlagStatus(DMA1_FLAG_TE7) != RESET)
    {
        DMA_ClearFlag(DMA1_FLAG_TE7);
        DMA_Cmd(DMA1_Channel7, DISABLE);
    }
}

/******************* (C) COPYRIGHT 2010 STMicroelectronics *******************/
